package com.xiaoma.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoma.dao.GoodsDao;
import com.xiaoma.feign.EsFeignService;
import com.xiaoma.feign.OrderFeignService;
import com.xiaoma.service.GoodsService;
import entity.Goods;
import entity.GoodsVo;
import entity.Payorder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import utils.OrderConstant;
import utils.OrderStatusEnum;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: xiaoma
 * @Date: 2021/7/9 10:03
 * @description:
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {

    private static final String incrementLikeKey =  "goods:like:";

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OrderFeignService orderFeignService;
    @Autowired
    private EsFeignService esFeignService;

    @Override
    @SentinelResource(value = "listGoods", blockHandler = "blockHandler")
    public List<Goods> listGoods() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean productGoods(Goods goods) {
        Goods dbGoods = goodsDao.selectById(goods.getId());
        if (dbGoods.getStockCount() < 0) return false;
        dbGoods.setStatus(1);
        if ((Integer) esFeignService.productGoods(dbGoods).get("code") == 0) return true;
        return false;
    }

    public List<Goods> blockHandler(BlockException ex) {
        log.error("原服务被降级");
        return null;
    }

    @Override
    public GoodsVo queryGoodsVoById(String id, String uid) {
        GoodsVo goodsVo = new GoodsVo();
        Goods goods = goodsDao.selectById(id);
        BeanUtils.copyProperties(goods, goodsVo);
        //生成防重复令牌 保证接口幂等性
        String token = UUID.randomUUID().toString().replace("-", "");
        //后端redis里存一个
        redisTemplate.opsForValue().set(OrderConstant.USER_ORDER_TOKEN_PREFIX + uid, token, 30, TimeUnit.MINUTES);
        //前端返回一个
        goodsVo.setOrderToken(token);
        return goodsVo;
    }

    @Override
    public Integer lockStock(String id, Integer num) {
        return goodsDao.lockStock(id, num);
    }

    @Override
    public void unlockStock(Payorder payorder) {
        //查询订单最新状态
        Payorder order = orderFeignService.queryById(payorder.getId());
        if (order == null) return;
        //如果当前订单状态为已取消
        if (OrderStatusEnum.CANCLED.getCode() == order.getOrderStatus()) {
            //解锁库存
            goodsDao.unlockStock(order.getGid().toString(), order.getGoodsNum());
        }
    }
    /**
     * @Description  商品点赞
     * @Author Xiaoma
     * @Date 2021/9/16 11:49
     * @param gid
     * @return java.lang.Long
     **/
    @Override
    public Long incrementLike(String gid) {
        Long num = redisTemplate.opsForValue().increment(incrementLikeKey + gid);
        return num;
    }


}
